package com.gupaoedu.vip.proxy.jdkproxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Auther: tcm
 * @Date: 2020/1/25 18:13
 * @Description:
 */
public class GoodsDaoProxy implements GPInvocationHandler {

    private GoodsDao goodsDao;

    public GoodsDaoProxy(GoodsDao goodsDao){
        this.goodsDao = goodsDao;
    }

    public Object getInstance(){
        Class<?> clazz = goodsDao.getClass();
        return GPProxy.newProxyInstance(new GPClassLoader(), clazz.getInterfaces(), this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object obj = method.invoke(goodsDao, args);
        arround(obj, args);
        return obj;
    }

    private void arround(Object obj, Object[] args){
        System.out.println(obj.toString() + "," + args);
    }
}
